package com.icedevcloud.datastruct.sort.bubblesort;

/**
 * 冒泡排序
 *
 * @author XiaoBingBy
 * @since 2020-12-14
 */
public class BubbleSort {

    /**
     * 对数组 params 进行冒泡排序
     *
     * @param params
     */
    public static void sort(Comparable[] params) {
        for (int i = 0; i < params.length; i++) {
            for (int j = 0; j < params.length - 1; j++) {
                System.out.println(String.format("i_%s=%s j_%s=%s", i, params[i], j, params[j]));
                if (!greater(params[i], params[j])) {
                    exch(params, i, j);
                }
            }
        }
    }

    /**
     * 比较大小
     *
     * @return
     */
    public static boolean greater(Comparable v, Comparable w) {
        return v.compareTo(w) > 0;
    }

    /**
     * 交换 params 数组 index i,j 位置
     *
     * @param params
     * @param i
     * @param j
     */
    public static void exch(Comparable[] params, int i, int j) {
        Comparable temp;
        temp = params[i];
        params[i] = params[j];
        params[j] = temp;
    }

}
